package com.its.common.log;

import com.its.common.log.config.LogConfig;
import com.yomahub.tlog.spring.TLogPropertyInit;
import com.yomahub.tlog.springboot.TLogCommonAutoConfiguration;
import com.yomahub.tlog.springboot.TLogFeignAutoConfiguration;
import com.yomahub.tlog.springboot.property.TLogProperty;
import com.yomahub.tlog.web.interceptor.TLogWebInvokeTimeInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.Ordered;

/**
 * @Author xiaxp
 * @Date 2021-12-21 22:25
 * @Description
 */
@Slf4j
@ComponentScan
@EnableConfigurationProperties({LogConfig.class, TLogProperty.class})
@AutoConfigureAfter(TLogCommonAutoConfiguration.class)
@AutoConfigureBefore(TLogFeignAutoConfiguration.class)
public class LogAutoConfigure implements Ordered {

    @Autowired
    private LogConfig logConfig;

    @Value("${spring.application.name}")
    private String appName;

    @Bean
    public TLogPropertyInit tLogPropertyInit(TLogProperty tLogProperty) {
        log.info(">>>>>>>>>>>>>>>>> " + appName + "  日志系统启动：成功! >>>>>>>>>>>>>>>>>");
        TLogPropertyInit tLogPropertyInit = new TLogPropertyInit();
        tLogPropertyInit.setPattern("[$preApp][$preIp][$spanId][$traceId]");
        tLogPropertyInit.setEnableInvokeTimePrint(logConfig.isEnableInvokeTimePrint());
        tLogPropertyInit.setIdGenerator(tLogProperty.getIdGenerator());
        tLogPropertyInit.setMdcEnable(tLogProperty.getMdcEnable());
        return tLogPropertyInit;
    }

    @Override
    public int getOrder() {
        return -100;
    }
}
